package com.sujing.autoUpdate.Servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sujing.autoUpdate.sqlOption.NewsVersionPath;

/**
 * Servlet implementation class newsVersion
 */
@WebServlet("/newsVersion")
public class NewsVersion extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsVersion() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String FileName=request.getParameter("FileName");
		String tempFileVersion=request.getParameter("FileVersion");
		System.out.println("FileName..............."+FileName);
		System.out.println("FileVersion................."+tempFileVersion);
		float FileVersion = Float.parseFloat(tempFileVersion);
		NewsVersionPath nvp=new NewsVersionPath();
		String newsVersionPath=nvp.NewsVersionPath(FileName, FileVersion);
		File file=new File(newsVersionPath);
		InputStream in=new FileInputStream(file);
		OutputStream out=response.getOutputStream();
		response.setContentType("application/x-msdownload");
		byte[] buffer=new byte[1024];
		int length=0;
		while ((length = in.read(buffer)) > 0) {
			out.write(buffer, 0, length);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
